package com.ruoyi.web.controller.storage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.purchase.domain.PoPurchase;
import com.ruoyi.purchase.service.PoPurchaseService;
import com.ruoyi.storage.domain.RepoRk;
import com.ruoyi.storage.domain.RepoWare;
import com.ruoyi.storage.service.RepoRkService;
import com.ruoyi.storage.service.RepoWareService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/storage/rk")
public class StorageRKController extends BaseController {
    @Autowired
    private RepoRkService repoRkService;

    @Autowired
    private RepoWareService repoWareService;

    @Autowired
    private PoPurchaseService poPurchaseService;

    @RequiresPermissions("storage:rk:view")
    @RequestMapping()
    public String view() {
        return "storage/rk/storage";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(RepoRk repoRk) {
        startPage();
        List<RepoRk> all = repoRkService.getAll(repoRk);
        return getDataTable(all);
    }

    @RequestMapping("/add")
    public String add(Model model) {
        List<String> pname = new ArrayList<>();
        List<RepoRk> all = repoRkService.getAll(new RepoRk());
        for (RepoRk repoRk : all) {
            pname.add(repoRk.getPoPurIndent());
        }
        List<PoPurchase> purId = poPurchaseService.list(new QueryWrapper<PoPurchase>().notIn("pur_id", pname));
        model.addAttribute("ps",purId);

        List<RepoWare> warelist = repoWareService.list();
        model.addAttribute("wares",warelist);
        model.addAttribute("user",getSysUser().getUserName());
        return "storage/rk/add";
    }

    @PostMapping("/addRe")
    @ResponseBody
    public AjaxResult addSave(@Validated RepoRk repoRk)
    {
        PoPurchase poPurchase = new PoPurchase();
        poPurchase.setPurId(repoRk.getPoPurIndent());
        poPurchase.setRkState(Integer.valueOf(repoRk.getReStatus()));
        return toAjax(repoRkService.save(repoRk)&&poPurchaseService.updateById(poPurchase));
    }

    @RequestMapping("editStatus")
    @ResponseBody
    public AjaxResult update_status(@Validated RepoRk repoRk) {
        PoPurchase poPurchase = new PoPurchase();
        poPurchase.setPurId(repoRkService.getById(repoRk.getRkId()).getPoPurIndent());
        poPurchase.setRkState(Integer.valueOf(repoRk.getReStatus()));
        return toAjax(repoRkService.updateById(repoRk)&&poPurchaseService.updateById(poPurchase));
    }

    @RequestMapping("/details/{id}")
    public String details(Model model,@PathVariable String id) {
        PoPurchase poPurchase = new PoPurchase();
        poPurchase.setPurId(id);
        List<PoPurchase> purList = poPurchaseService.findPurList(poPurchase);
        RepoWare repoWare = new RepoWare();
        RepoRk repoRk1 = new RepoRk();
        repoRk1.setPoPurIndent(id);
        List<RepoRk> repoRks = repoRkService.getAll(repoRk1);
        repoWare.setWareId(repoRks.get(0).getRepoWareId());
        List<RepoWare> repoWares = repoWareService.getList(repoWare);
        model.addAttribute("purList",purList);
        model.addAttribute("repoWare",repoWares);
        model.addAttribute("rks",repoRks);
        return "storage/rk/detail";
    }
}
